<?php
require_once 'common.php';

$excel = ROOT_PATH . "/www/loginIds_2.xls";
$data = MooObj::get('Control_ExcelConfMaker_Excel')->read($excel, 0);

/*
$data = array(
	0 => array(
		'server' => 2,
  		'playerId' => 428
	),	
	1 => array(
		'server' => 3,
  		'playerId' => 426
	),	
);
*/

$dbConfs = array(
	1 => array(
	 	'host' => '10.10.134.8:3306',	
	 	'user' => 'root',	
	 	'pwd' => '@fox0426^feihu^db',	
	 	'dbName' => 'qmby',	
	 	'char' => 'utf8',	
	),
	2 => array(
	 	'host' => '10.10.128.139:3306',	
	 	'user' => 'root',	
	 	'pwd' => '@fox0426^feihu^db',	
	 	'dbName' => 'qmby',	
	 	'char' => 'utf8',	
	),
	3 => array(
	 	'host' => '10.10.113.17:3306',	
	 	'user' => 'root',	
	 	'pwd' => '@fox0426^feihu^db',	
	 	'dbName' => 'qmby',	
	 	'char' => 'utf8',	
	),
);

/* $conf = array();
$conf['host'] 	= 'localhost:3306';
$conf['user'] 	= 'root';
$conf['pwd']  	= '';
$conf['dbName'] = 'qmby_dev';
$conf['char'] 	= 'utf8'; */

$date = $_REQUEST['date'];
if(empty($date)) {
	$date = "2015-08-18";
}
// 查询登陆日期大于17号的
$time 	= $date ." 00:00:00";
foreach ($data as $key => $val) {
	$server = $val['server'];
	$conf = $dbConfs[$server];

	$link = mysql_connect($conf['host'], $conf['user'], $conf['pwd']);
	if (!$link) {
		echo '数据库连接失败!' . mysql_error();
		exit;
	}
	
	if (!@mysql_select_db($conf['dbName'])) {
		echo '选择数据库失败' . mysql_error();
		exit;
	}
	mysql_set_charset($conf['char']);
	
	$playerId = $val['playerId'];
	$sql = "select id,name,loginTime,createTime from player where id = " . $playerId;
	
	$result = mysql_query($sql, $link);
	
	while($res = mysql_fetch_array($result)) {
		$i++;
		$arr['0'] = $i;
		$arr['1'] = $server;
		$arr['2'] = $res['id'];
		$arr['3'] = $res['name'];
		$arr['4'] = $res['createTime'];
		$arr['5'] = $res['loginTime'];
		
		if($res['loginTime'] >= $time) {
			$arr['6'] = 1; // 登陆了
		} else {
			$arr['6'] = 0; // 未登陆了
		}
		
		$rsIds[] = $arr;
	}
}

// 标题框
$excelRs = array(
		0 => array(
			0 => '序号',
			1 => '服',
			2 => 'playerId',
			3 => '角色名',
			4 => '注册时间',
			5 => '最后登陆时间',
			6 => '是否在'.$time.'后登陆',
		),
);

$excelRs = array_merge($excelRs, $rsIds);
$writeExcelArr[0]['name'] = '17号之后登陆用户数据核对';
$writeExcelArr[0]['data'] = $excelRs;

$rs = MooObj::get('Control_ExcelConfMaker_Excel')->write('loginCheck_2.xls', $writeExcelArr);

$filepath = ROOT_PATH . "/www/loginCheck_2.xls";

downFile($filepath, 'loginCheck_2.xls');

function downFile($filepath, $fileName) {
	$fp			=	fopen($filepath,"r");
	$file_size	=	filesize($filepath);
	//下载文件需要用到的头
	header("Content-type: application/octet-stream");
	header("Accept-Ranges: bytes");
	header("Accept-Length:".$file_size);
	header("Content-Disposition: attachment; filename=".$fileName);
	$buffer		=	1024;
	$file_count	=	0;
	//向浏览器返回数据
	while(!feof($fp) && $file_count < $file_size){
		$file_con	=	fread($fp,$buffer);
		$file_count	+=	$buffer;
		echo $file_con;
	}
	fclose($fp);
}



